
<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="/admin/lib/html5shiv.js"></script>
    <script type="text/javascript" src="/admin/lib/respond.min.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="/admin/static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="/admin/lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="/admin/static/h-ui.admin/css/style.css" />
    <!--[if IE 6]>
    <script type="text/javascript" src="/admin/lib/DD_belatedPNG_0.0.8a-min.js" ></script>
    <script>DD_belatedPNG.fix('*');</script>
    <![endif]-->
    <!--/meta 作为公共模版分离出去-->

    <title>修改商品</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
</head>
<body>
<div id="tab_demo" class="HuiTab">
    <div class="tabBar clearfix"><span>基本信息</span><span>轮播图</span><span>商品属性</span></div>
    <div class="tabCon">
        <article class="page-container">
            <form action="/admin/item/edit" method="post" class="form form-horizontal" id="form-member-add">
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品名称：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <input type="text" class="input-text" placeholder="" id="item_name" name="item_name" value="{{$data -> item_name}}">
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品分类：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <span class="select-box" style="width:150px;">
                            <select class="select" name="classification_id" size="1">
                                <option value="0">请选择分类</option>
                                @foreach($class as $val)
                                    @if($data -> classification_id == $val -> id)
                                        <option value="{{$val -> id}}" selected>{{$val -> classification_name}}</option>
                                    @else
                                        <option value="{{$val -> id}}">{{$val -> classification_name}}</option>
                                    @endif

                                @endforeach
                            </select>
				        </span>
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品主图：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        @if($data -> cover_img == '')
                            <div id="uploader-demo">
                                <div id="fileList" class="uploader-list"></div>
                                <div id="filePicker">选择图片</div>
                            </div>
                        @else
                            <img src="{{$data -> cover_img}}" id="show_cover" style="width: 100px;">
                            <div id="uploader-demo">
                                <div id="fileList" class="uploader-list"></div>
                                <div id="filePicker">更改图片</div>
                            </div>
                        @endif
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>商品价格：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <input type="text" class="input-text" placeholder="0" name="item_price" id="item_price" value="{{$data -> item_price}}">
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>市场价：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <input type="text" class="input-text" placeholder="0" name="item_mprice" id="item_mprice" value="{{$data -> item_mprice}}">
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>进货价：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <input type="text" class="input-text" placeholder="0" name="item_cprice" id="item_cprice" value="{{$data -> item_cprice}}">
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>总库存：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <input type="text" class="input-text" placeholder="0" name="stock" id="stock" value="{{$data -> stock}}">
                    </div>
                </div>
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>排序：</label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <input type="text" class="input-text" placeholder="0" name="sort" id="sort" value="{{$data -> sort}}">
                    </div>
                </div>
                <input type="hidden" name="id" value="{{$data -> id}}">
                <input type="hidden" name="status" value="{{$data -> status}}">
                <input type="hidden" name="cover_img" value="{{$data -> cover_img}}">
                {{csrf_field()}}
                <div class="row cl">
                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                        <input class="btn btn-primary radius" type="submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
                    </div>
                </div>
            </form>
        </article>
    </div>
    <div class="tabCon">
        <article class="page-container">
            <form class="form form-horizontal">
                @foreach($Img_list as $val)
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"></label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <img src="{{$val -> img}}" alt="" style="width: 100px;"><a href="javascript:void(0);" onclick="del_img(this,'{{$val -> id}}')"><i class="Hui-iconfont">&#xe609;</i></a>
                    </div>
                </div>
                @endforeach
                <div class="row cl">
                    <label class="form-label col-xs-4 col-sm-3"></label>
                    <div class="formControls col-xs-8 col-sm-9">
                        <div id="uploader-demo-add">
                            <div id="fileList-add" class="uploader-list"></div>
                            <div id="filePicker-add">添加图片</div>
                        </div>
                    </div>
                </div>
            </form>
        </article>
    </div>
    <div class="tabCon">
        <article class="page-container">
            <button class="btn btn-success-outline size-S radius" onClick="modaldemo()">添加属性</button>
            <button class="btn btn-secondary-outline size-S radius" onClick="modaldemo1()">添加库存</button>
            <form class="form form-horizontal">
                @foreach($stock as $val)
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3"><a href="javascript:void(0);" onclick="edit_attr_img(this,'{{$val['id']}}')"><img src="{{$val['item_img']}}" alt="" style="width: 100px;"></a></label>
                        <div class="formControls col-xs-8 col-sm-9">
                            添加时间：{{$val['created_at']}}&emsp;&emsp;
                            <a href="javascript:void(0);" onclick="del_stock(this,'{{$val['id']}}')"><i class="Hui-iconfont">&#xe609;</i></a>
                            <br/>
                            属性：
                            @foreach($val['item_attribute_ids'] as $attr_val)
                                <span style="color: #5eb95e">{{$attr_val}}</span>
                            @endforeach
                            <br/>
                            库存：
                            <div class="btn-group">
                                <span class="btn btn-primary radius" onclick="reduce_stock(this,'{{$val['id']}}')">-</span>
                                <span class="btn btn-default radius" style="padding: 0;"><input type="text" class="input-text" name="attr_stock" value="{{$val['stock']}}" style="text-align: center;margin: 0;width: 40px;height: 100%;" value-id="{{$val['id']}}"></span>
                                <span class="btn btn-primary radius" onclick="plus_stock(this,'{{$val['id']}}')">+</span>
                            </div>
                        </div>
                    </div>
                @endforeach
                <div id="uploader-demo-edit-attr-img" style="display: none;">
                    <div id="fileList-edit-attr-img" class="uploader-list"></div>
                    <div id="filePicker-edit-attr-img">添加图片</div>
                </div>
                    <input type="hidden" name="item_stock_id" value="">
            </form>
        </article>
    </div>
</div>


<div id="modal-demo" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content radius">
            <div class="modal-header">
                <h3 class="modal-title">添加属性</h3>
                <a class="close" data-dismiss="modal" aria-hidden="true" href="javascript:void(0);">×</a>
            </div>
            <div class="modal-body">
                <form class="form form-horizontal">
                    @foreach($attribute as $val)
                        <div class="row cl">
                            <label class="form-label col-xs-4 col-sm-3">{{$val -> attribute_name}}</label>
                            <div class="formControls col-xs-8 col-sm-9">
                                <div class="skin-minimal">
                                @foreach($val -> attribute_values as $value)
                                    <div class="check-box">
                                        <input type="checkbox" id="checkbox-{{$value}}" value-attribute_id="{{$val -> id}}" value="{{$value}}"
                                            @foreach($item_attribute as $ival)
                                            @if($val -> id == $ival -> attribute_id && $value == $ival -> attribute_value)
                                                checked name="edit_attribute_value" value-i-attr_id="{{$ival -> id}}"
                                            @endif
                                            @endforeach
                                        >
                                        <label for="checkbox-{{$value}}">{{$value}}</label>
                                    </div>
                                @endforeach
                                </div>
                            </div>
                        </div>
                    @endforeach
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

<div id="modal-demo1" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content radius">
            <div class="modal-header">
                <h3 class="modal-title">添加库存</h3>
                <a class="close" data-dismiss="modal" aria-hidden="true" href="javascript:void(0);">×</a>
            </div>
            <div class="modal-body">
                <form class="form form-horizontal">
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3">商品属性</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            @foreach($array_attr as $val)
                                <span class="select-box radius mb-20">
                                    <select class="select" size="1" name="item_attribute_ids">
                                        <option value="0">请选择属性</option>
                                        @foreach($val as $value)
                                            <option value="{{$value['id']}}">{{$value['value']}}</option>
                                        @endforeach
                                    </select>
                                </span>
                            @endforeach
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3">库存数</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <input type="text" placeholder="库存数" name="i_attr_stock" class="input-text radius size-S mb-20" value="1">
                        </div>
                    </div>
                    <div class="row cl">
                        <label class="form-label col-xs-4 col-sm-3">图片</label>
                        <div class="formControls col-xs-8 col-sm-9">
                            <div id="uploader-demoa">
                                <div id="fileLista" class="uploader-list"></div>
                                <div id="filePickera">添加图片</div>
                            </div>
                            <input type="hidden" name="item_stoct_img" value="">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" onclick="add_stock()">确定</button>
                <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="/admin/lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="/admin/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/admin/static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="/admin/static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script>
<script type="text/javascript" src="/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<!--引入webuploader相关文件-->
<link rel="stylesheet" type="text/css" href="/admin/webuploader-0.1.5/webuploader.css">
<script type="text/javascript" src="/admin/webuploader-0.1.5/webuploader.js"></script>
<!--引入webuploader相关文件-->

<script type="text/javascript">
    var page = localStorage.tab_show_id == undefined ? "0" : localStorage.tab_show_id;
    jQuery.Huitab =function(tabBar,tabCon,class_name,tabEvent,i){
        var $tab_menu=$(tabBar);
        // 初始化操作
        $tab_menu.removeClass(class_name);
        $(tabBar).eq(i).addClass(class_name);
        $(tabCon).hide();
        $(tabCon).eq(i).show();

        $tab_menu.bind(tabEvent,function(){
            $tab_menu.removeClass(class_name);
            $(this).addClass(class_name);
            var index=$tab_menu.index(this);
            $(tabCon).hide();
            $(tabCon).eq(index).show();
        })
    }
    $.Huitab("#tab_demo .tabBar span","#tab_demo .tabCon","current","click",page);

    jQuery(function() {
        var $ = jQuery,
                $list = $('#fileList'),
                $list_add = $('#fileList-add'),
                $list_edit_attr_img = $('#fileList-edit-attr-img'),
                $lista = $('#fileLista'),
                // 优化retina, 在retina下这个值是2
                ratio = window.devicePixelRatio || 1,
                // 缩略图大小
                thumbnailWidth = 100 * ratio,
                thumbnailHeight = 100 * ratio,
                // Web Uploader实例
                uploader,
                uploader_add,
                uploader_edit_attr_img,
                uploadera;

        // 初始化Web Uploader
        uploader = WebUploader.create({
            formData:{
                '_token': "{{csrf_token()}}",
                'img_type':'cover_img',
                'qiniu_type':'qiniu_item'
            },
            auto: true,
            swf: '/admin/webuploader-0.1.5/Uploader.swf',
            server: '/admin/upload/qiniu',
            pick: '#filePicker',
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
            }
        });
        uploader_add = WebUploader.create({
            formData:{
                '_token': "{{csrf_token()}}",
                'qiniu_type':'qiniu_item'
            },
            auto: true,
            swf: '/admin/webuploader-0.1.5/Uploader.swf',
            server: '/admin/upload/qiniu',
            pick: '#filePicker-add',
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
            }
        });
        uploader_edit_attr_img = WebUploader.create({
            formData:{
                '_token': "{{csrf_token()}}",
                'qiniu_type':'qiniu_item'
            },
            auto: true,
            swf: '/admin/webuploader-0.1.5/Uploader.swf',
            server: '/admin/upload/qiniu',
            pick: '#filePicker-edit-attr-img',
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
            }
        });
        uploadera = WebUploader.create({
            formData:{
                '_token': "{{csrf_token()}}",
                'qiniu_type':'qiniu_item'
            },
            auto: true,
            swf: '/admin/webuploader-0.1.5/Uploader.swf',
            server: '/admin/upload/qiniu',
            pick: '#filePickera',
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
            }
        });
        // 当有文件添加进来的时候
        uploader.on( 'fileQueued', function( file ) {
            var $li = $(
                            '<div id="' + file.id + '" class="file-item thumbnail"><img><div class="info">' + file.name + '</div></div>'
                    ),
                    $img = $li.find('img');
            //清除之前的预览区，只保留一个预览图
            $('.thumbnail').remove();
            $list.append( $li );
            // 创建缩略图
            uploader.makeThumb( file, function( error, src ) {
                if ( error ) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }
                $img.attr( 'src', src );
            }, thumbnailWidth, thumbnailHeight );
        });
        uploader_add.on( 'fileQueued', function( file ) {
            var $li = $('<div id="' + file.id + '" class="file-item thumbnai2"><img><div class="info">' + file.name + '</div></div>'),
                    $img = $li.find('img');
            $('.thumbnai2').remove();
            $list_add.append( $li );
            // 创建缩略图
            uploader_add.makeThumb( file, function( error, src ) {
                if ( error ) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }
                $img.attr( 'src', src );
            }, thumbnailWidth, thumbnailHeight );
        });
        uploadera.on( 'fileQueued', function( file ) {
            var $li = $('<div id="' + file.id + '" class="file-item thumbnai3"><img><div class="info">' + file.name + '</div></div>'),
                    $img = $li.find('img');
            $('.thumbnai3').remove();
            $lista.append( $li );
            // 创建缩略图
            uploadera.makeThumb( file, function( error, src ) {
                if ( error ) {
                    $img.replaceWith('<span>不能预览</span>');
                    return;
                }
                $img.attr( 'src', src );
            }, thumbnailWidth, thumbnailHeight );
        });
        // 文件上传过程中创建进度条实时显示。
        uploader.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                    $percent = $li.find('.progress span');
            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<p class="progress"><span></span></p>')
                        .appendTo( $li )
                        .find('span');
            }
            $percent.css( 'width', percentage * 100 + '%' );
        });
        uploader_add.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                    $percent = $li.find('.progress span');
            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<p class="progress"><span></span></p>')
                        .appendTo( $li )
                        .find('span');
            }
            $percent.css( 'width', percentage * 100 + '%' );
        });
        uploadera.on( 'uploadProgress', function( file, percentage ) {
            var $li = $( '#'+file.id ),
                    $percent = $li.find('.progress span');
            // 避免重复创建
            if ( !$percent.length ) {
                $percent = $('<p class="progress"><span></span></p>')
                        .appendTo( $li )
                        .find('span');
            }
            $percent.css( 'width', percentage * 100 + '%' );
        });
        // 文件上传成功，给item添加成功class, 用样式标记上传成功。，其第2个参数是ajax返回值
        uploader.on( 'uploadSuccess', function( file,data ) {
            $( '#'+file.id ).addClass('upload-state-done');
            //将路径给放到隐藏域中
            $('input[name=cover_img]').val(data.path);
            layer.msg(data.message,{icon: '6'});
        });
        uploader_add.on( 'uploadSuccess', function( file,data ) {
            $( '#'+file.id ).addClass('upload-state-done');
            layer.msg(data.message,{icon: '6'});
            //将图片添加至数据库
            $.ajax({
                type : 'POST',
                url  : '/admin/item_img/add',
                data :{item_id:$('input[name=id]').val(),img:data.path,'_token': "{{csrf_token()}}",},
                dataType : 'json',
                success : function(res){
                    if(res == 1){
                        localStorage.tab_show_id = "1";
                        layer.msg('添加商品轮播图成功!', {
                            icon: 1,
                            time: 1000
                        });
                        var wait = 1;
                        var interval = setInterval(function(){
                            var time = -- wait;
                            if(time <= 0) {
                                location.replace(location.href);
                                clearInterval(interval);
                            };
                        }, 1000);
                    }else{
                        layer.msg('添加商品轮播图失败!', {
                            icon: 2,
                            time: 1000
                        });
                    }
                },
                error:function(xhr){

                }
            });

        });
        uploader_edit_attr_img.on( 'uploadSuccess', function( file,data ) {
            $( '#'+file.id ).addClass('upload-state-done');
            layer.msg(data.message,{icon: '6'});
            $.ajax({
                type : 'POST',
                url  : '/admin/item_stock/edit_img',
                data :{id:$('input[name=item_stock_id]').attr('value-id'),img:data.path,'_token': "{{csrf_token()}}",},
                dataType : 'json',
                success : function(res){
                    if(res == 1){
                        localStorage.tab_show_id = "2";
                        layer.msg('修改商品属性图成功!', {
                            icon: 1,
                            time: 1000
                        });
                        var wait = 1;
                        var interval = setInterval(function(){
                            var time = -- wait;
                            if(time <= 0) {
                                location.replace(location.href);
                                clearInterval(interval);
                            };
                        }, 1000);
                    }else{
                        layer.msg('修改商品属性图失败!', {
                            icon: 2,
                            time: 1000
                        });
                    }
                },
                error:function(xhr){

                }
            });

        });
        uploadera.on( 'uploadSuccess', function( file,data ) {
            $( '#'+file.id ).addClass('upload-state-done');
            //将路径给放到隐藏域中
            $('input[name=item_stoct_img]').val(data.path);
            layer.msg(data.message,{icon: '6'});
        });
        // 文件上传失败，现实上传出错。
        uploader.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                    $error = $li.find('div.error');
            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }
            $error.text('上传失败');
        });
        uploader_add.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                    $error = $li.find('div.error');
            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }
            $error.text('上传失败');
        });
        uploader_edit_attr_img.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                    $error = $li.find('div.error');
            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }
            $error.text('上传失败');
        });
        uploadera.on( 'uploadError', function( file ) {
            var $li = $( '#'+file.id ),
                    $error = $li.find('div.error');
            // 避免重复创建
            if ( !$error.length ) {
                $error = $('<div class="error"></div>').appendTo( $li );
            }
            $error.text('上传失败');
        });
        // 完成上传完了，成功或者失败，先删除进度条。
        uploader.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').remove();
        });
        uploader_add.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').remove();
        });
        uploadera.on( 'uploadComplete', function( file ) {
            $( '#'+file.id ).find('.progress').remove();
        });
    });


    $(function(){
        $('.skin-minimal input').iCheck({
            checkboxClass: 'icheckbox-blue',
            radioClass: 'iradio-blue',
            increaseArea: '20%'
        });

        $("#form-member-add").validate({
            rules:{
                classification_id:{
                    required:true,
                },
                item_name:{
                    required:true,
                    minlength:2,
                    maxlength:16
                }
            },
            onkeyup:false,
            focusCleanup:true,
            success:"valid",
            submitHandler:function(form){
                //$(form).ajaxSubmit();
                $(form).ajaxSubmit({
                    type: 'post',
                    success: function(data) {
                        if(data == 1){
                            layer.msg('修改成功!', {
                                icon: 1,
                                time: 1000
                            });
                            var wait = 1;
                            var interval = setInterval(function(){
                                var time = -- wait;
                                if(time <= 0) {
                                    location.replace(location.href);
                                    clearInterval(interval);
                                };
                            }, 1000);
                        }else{
                            layer.msg('修改失败!', {
                                icon: 2,
                                time: 1000
                            });
                        }

                    },
                    error: function(XmlHttpRequest, textStatus, errorThrown) {
                        layer.msg('error!', {
                            icon: 1,
                            time: 1000
                        });
                    }
                });
                // var index = parent.layer.getFrameIndex(window.name);
                // //parent.$('.btn-refresh').click();
                // parent.layer.close(index);
            }
        });


        $('input[name=attr_stock]').change(function(){
            var _this = $(this);
            $.ajax({
                type : 'POST',
                url  : '/admin/item_stock/edit_stock',
                data :{'_token': "{{csrf_token()}}",id:$(this).attr('value-id'),type:'edit',stock:$(this).val()},
                dataType : 'json',
                success : function(res){
                    if(res == 1){
                        layer.msg('添加库存成功!', {
                            icon: 1,
                            time: 1000
                        });
                        var attr_stock = $(_this).val();
                        $(_this).val(Number(attr_stock) + 1);
                    }else{
                        layer.msg('添加库存失败!', {
                            icon: 2,
                            time: 1000
                        });
                    }
                },
                error:function(xhr){

                }
            });

        });

        $('.iCheck-helper').click(function(){
            var _this = $(this);
            var checked_status = $(this).prev('input').attr('checked');
            var item_id = $('input[name=id]').val();
            if(checked_status == undefined){
                //如果是添加属性
                var attribute_id = $(this).prev('input').attr('value-attribute_id');
                var attribute_value = $(this).prev('input').val();
                $.ajax({
                    type : 'POST',
                    url  : '/admin/item_attribute/add',
                    data : {'_token': "{{csrf_token()}}",item_id:item_id,attribute_id:attribute_id,attribute_value:attribute_value},
                    dataType : 'json',
                    success : function(res){
                        if(res == 1){
                            layer.msg('添加属性成功!', {
                                icon: 1,
                                time: 1000
                            });
                            $(this).prev('input').attr('checked','checked');
                        }else{
                            layer.msg('添加属性失败!', {
                                icon: 2,
                                time: 1000
                            });
                        }
                        var wait = 1;
                        var interval = setInterval(function(){
                            var time = -- wait;
                            if(time <= 0) {
                                location.replace(location.href);
                                clearInterval(interval);
                            };
                        }, 1000);
                    },
                    error:function(xhr){

                    }
                });
            }else if(checked_status == 'checked'){
                //如果是删除属性
                var id = $(this).prev('input').attr('value-i-attr_id');
                $.ajax({
                    type : 'POST',
                    url  : '/admin/item_attribute/del',
                    data : {'_token': "{{csrf_token()}}",id:id,item_id:item_id},
                    dataType : 'json',
                    success : function(res){
                        if(res == 1){
                            layer.msg('删除属性成功!', {
                                icon: 1,
                                time: 1000
                            });
                            $(_this).prev('input').removeAttr('checked');
                        }else{
                            layer.msg('删除属性失败!', {
                                icon: 2,
                                time: 1000
                            });
                        }
                        var wait = 1;
                        var interval = setInterval(function(){
                            var time = -- wait;
                            if(time <= 0) {
                                location.replace(location.href);
                                clearInterval(interval);
                            };
                        }, 1000);
                    },
                    error:function(xhr){

                    }
                });
            }

        });
    });

    function del_img(obj,id){
        $.ajax({
            type : 'POST',
            url  : '/admin/item_img/del',
            data :{'_token': "{{csrf_token()}}",id:id},
            dataType : 'json',
            success : function(res){
                if(res == 1){
                    layer.msg('删除商品轮播图成功!', {
                        icon: 1,
                        time: 1000
                    });
                    $(obj).closest('.row').remove();
                }else{
                    layer.msg('删除商品轮播图失败!', {
                        icon: 2,
                        time: 1000
                    });
                }
            },
            error:function(xhr){

            }
        });
    }

    function plus_stock(obj,id){
        $.ajax({
            type : 'POST',
            url  : '/admin/item_stock/edit_stock',
            data :{'_token': "{{csrf_token()}}",id:id,type:'plus'},
            dataType : 'json',
            success : function(res){
                if(res == 1){
                    layer.msg('添加库存成功!', {
                        icon: 1,
                        time: 1000
                    });
                    var attr_stock = $(obj).closest('.row').find('input[name=attr_stock]').val();
                    $(obj).closest('.row').find('input[name=attr_stock]').val(Number(attr_stock) + 1);
                }else{
                    layer.msg('添加库存失败!', {
                        icon: 2,
                        time: 1000
                    });
                }
            },
            error:function(xhr){

            }
        });
    }

    function reduce_stock(obj,id){
        if($('input[name=attr_stock]').val() == 0){
            layer.msg('库存已为0，不能再减少!');
            return false;
        }
        $.ajax({
            type : 'POST',
            url  : '/admin/item_stock/edit_stock',
            data :{'_token': "{{csrf_token()}}",id:id,type:'reduce'},
            dataType : 'json',
            success : function(res){
                if(res == 1){
                    layer.msg('减少库存成功!', {
                        icon: 1,
                        time: 1000
                    });
                    var attr_stock = $(obj).closest('.row').find('input[name=attr_stock]').val();
                    $(obj).closest('.row').find('input[name=attr_stock]').val(Number(attr_stock) - 1);
                }else{
                    layer.msg('减少库存失败!', {
                        icon: 2,
                        time: 1000
                    });
                }
            },
            error:function(xhr){

            }
        });
    }

    function edit_attr_img(obj,id){
        $('input[name=item_stock_id]').attr('value-id',id);
        $('#uploader-demo-edit-attr-img').find('input').click();
    }

    function modaldemo(){
        $("#modal-demo").modal("show");
    }
    function modaldemo1(){
        $("#modal-demo1").modal("show");
    }

    function add_stock(){
        var item_id = $('input[name=id]').val(),attr_ids = '';
        var stock = $('input[name=i_attr_stock]').val();
        var item_img = $('input[name=item_stoct_img]').val();
        $('select[name=item_attribute_ids]').each(function(){
            attr_ids += $(this).val() + ',';
        });
        if(stock <= 0){layer.msg('库存数不能小于等于0!'); return false;}
        if(item_img == ''){layer.msg('必须上传商品属性图片!'); return false;}
        $.ajax({
            type : 'POST',
            url  : '/admin/item_stock/add',
            data : {'_token': "{{csrf_token()}}",item_id:item_id,stock:stock,attr_ids:attr_ids,item_img:item_img},
            dataType : 'json',
            success  : function(res){
                if(res == 1) {
                    layer.msg('新增商品库存成功!', {
                        icon: 1,
                        time: 1000
                    });
                    var wait = 1;
                    var interval = setInterval(function(){
                        var time = -- wait;
                        if(time <= 0) {
                            location.replace(location.href);
                            clearInterval(interval);
                        };
                    }, 1000);
                }else{
                    layer.msg('新增商品库存失败!', {
                        icon: 2,
                        time: 1000
                    });
                }
            },
            error:function(xhr){

            }
        });
    }

    function del_stock(obj,id){
        $.ajax({
            type : 'POST',
            url  : '/admin/item_stock/del',
            data : {'_token': "{{csrf_token()}}",id:id},
            dataType : 'json',
            success  : function(res){
                if(res == 1) {
                    layer.msg('删除商品属性库存成功!', {
                        icon: 1,
                        time: 1000
                    });
                    $(obj).closest('.row').remove();
                }else{
                    layer.msg('新增商品库存失败!', {
                        icon: 2,
                        time: 1000
                    });
                }
            },
            error:function(xhr){

            }
        });
    }
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>